home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 1995 #3 & #4 / Amiga Plus CD - 1995 - No. 3 and 4.iso / pd / sound / cybersound / 14bit.driver / calibration_source / calibrate.c next >
C/C++ Source or Header  |  1995-07-20  |  26KB  |  1,197 lines

  1.  
  2. /*****************************************************************************
  3.  *
  4.  * CyberSound: 14 Bit sound driver calibration program
  5.  *
  6.  * (c) 1995 by Christian Buchner
  7.  *
  8.  *****************************************************************************
  9.  *
  10.  * Calibrate.c
  11.  */
  12.  
  13.  
  14. #include <intuition/intuition.h>
  15. #include <libraries/gadtools.h>
  16. #include <libraries/asl.h>
  17. #include <graphics/text.h>
  18. #include <graphics/gfxbase.h>
  19. #include <proto/intuition.h>
  20. #include <proto/graphics.h>
  21. #include <proto/dos.h>
  22. #include <proto/exec.h>
  23. #include <proto/gadtools.h>
  24. #include <proto/icon.h>
  25. #include <proto/asl.h>
  26. #include <devices/audio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <stdio.h>
  30. #include <stdarg.h>
  31. #include <hardware/custom.h>
  32. #include <hardware/intbits.h>
  33. #include <hardware/dmabits.h>
  34. #include <hardware/adkbits.h>
  35. #include <hardware/cia.h>
  36. #include <exec/execbase.h>
  37. #include <workbench/workbench.h>
  38.  
  39.  
  40. /* Prototypes */
  41.  
  42. void OpenConfig(void);
  43. void SaveAs(void);
  44. void Quit(void);
  45. void ResetToDefaults(void);
  46. void LastSaved(void);
  47. void Restore(void);
  48. void IconToggle(void);
  49.  
  50. BOOL OpenLibs(void);
  51. void CloseLibs(void);
  52.  
  53. BOOL InitScreenWindow(void);
  54. void DeinitScreenWindow(void);
  55. void Resize(void);
  56. void FreeButtons(void);
  57. void DrawBar(struct Window *Window, UBYTE bar, BYTE value, BYTE oldvalue, UWORD pen);
  58.  
  59. BOOL AllocAudio(void);
  60. void FreeAudio(void);
  61. void StartCalibSound(ULONG Frequency);
  62. void StopCalibSound(void);
  63.  
  64. BOOL LoadData(UBYTE *Dirname, UBYTE *Filename);
  65. BOOL SaveData(UBYTE *EnvDir, UBYTE *Dirname, UBYTE *Filename, BOOL CreateIcon);
  66. void DefaultData(void);
  67. void ShowData(void);
  68.  
  69. LONG __stdargs Message(UBYTE *Msg,UBYTE *Options,...);
  70.  
  71.  
  72.  
  73. /* Globals */
  74.  
  75. UBYTE Version[]="$VER: 14Bit Calibration 1.1 "__AMIGADATE__" by Christian Buchner";
  76.  
  77. UWORD __chip HighSample;
  78. UWORD __chip LowSample;
  79.  
  80. BYTE AdditiveArray[256];        /* Set last value to 0x7F */
  81.  
  82. BYTE *Array=AdditiveArray;
  83.  
  84. struct Process *MyProc;
  85. struct WBStartup *WBenchMsg;
  86.  
  87. struct DosLibrary *DOSBase;
  88. struct GfxBase *GfxBase;
  89. struct IntuitionBase *IntuitionBase;
  90. struct Library *GadToolsBase;
  91. struct Library *AslBase;
  92. struct Library *IconBase;
  93.  
  94. struct Screen *Screen;
  95. APTR VisualInfo;
  96. struct TextAttr *Font, Attr;
  97. UWORD FontX, FontY;
  98. UWORD GadHeight;
  99. UWORD GadSpace;
  100. struct Window *Window;
  101. struct Window *OldWindowPtr;
  102. struct Menu *Menu;
  103. struct Gadget *HighGad,*LowGad;
  104. struct Gadget *Context;
  105. BOOL Active=TRUE;
  106. UWORD pos=128;
  107. UWORD AreaLeft;
  108. UWORD AreaTop;
  109. UWORD AreaWidth;
  110. UWORD AreaHeight;
  111. UBYTE buffer1[20];
  112. UBYTE buffer2[20];
  113. UBYTE Drawer[160];
  114. UBYTE File[40];
  115.  
  116. BOOL CreateIcons=FALSE;
  117.  
  118. struct MsgPort *AudioReply;
  119. struct IOAudio *AudioRequest;
  120. ULONG Freq=1000;
  121.  
  122.  
  123. struct NewMenu CalibNewMenu[]=
  124. {
  125.     /* nm_Type    nm_Label            nm_CommKey    nm_Flags            nm_MutualExclude    nm_UserData */
  126.     NM_TITLE,    "Project",            NULL,        0,                    0,                    NULL,
  127.     NM_ITEM,    "Open...",            "0",        0,                    0,                    &OpenConfig,
  128.     NM_ITEM,    "Save As...",        "A",        0,                    0,                    &SaveAs,
  129.     NM_ITEM,    NM_BARLABEL,        NULL,        0,                    0,                    NULL,
  130.     NM_ITEM,    "Quit",                "Q",        0,                    0,                    &Quit,
  131.     NM_TITLE,    "Edit",                NULL,        0,                    0,                    NULL,
  132.     NM_ITEM,    "Reset To Defaults","D",        0,                    0,                    &ResetToDefaults,
  133.     NM_ITEM,    "Last Saved",        "L",        0,                    0,                    &LastSaved,
  134.     NM_ITEM,    "Restore",            "R",        0,                    0,                    &Restore,
  135.     NM_TITLE,    "Settings",            NULL,        0,                    0,                    NULL,
  136.     NM_ITEM,    "Create Icons?",    "I",        CHECKIT|MENUTOGGLE,    0,                    &IconToggle,
  137.     NM_END,        NULL,                NULL,        0,                    0,                    NULL
  138. };
  139.  
  140. #define NEWICONINDEX 10        /* Index of "Create Icons?" NewMenu item */
  141. #define ICONITEM 0xF802        /* Number of "Create Icons?" Item */
  142.  
  143.  
  144. UWORD ImageData[4*0x17*2]=
  145. {
  146.     0x0000,0x0000,0x0004,0x0000,
  147.     0x0000,0x0000,0x0001,0x0000,
  148.     0x0000,0x07FF,0x8000,0x4000,
  149.     0x0000,0x1800,0x6000,0x1000,
  150.     0x0000,0x20FC,0x1000,0x0800,
  151.     0x0000,0x4102,0x0800,0x0C00,
  152.     0x0000,0x4082,0x0800,0x0C00,
  153.     0x0000,0x4082,0x0800,0x0C00,
  154.     0x0000,0x2104,0x0800,0x0C00,
  155.     0x0000,0x1E18,0x1000,0x0C00,
  156.     0x0000,0x0060,0x2000,0x0C00,
  157.     0x0000,0x0080,0xC000,0x0C00,
  158.     0x0000,0x0103,0x0000,0x0C00,
  159.     0x0000,0x021C,0x0000,0x0C00,
  160.     0x0000,0x0108,0x0000,0x0C00,
  161.     0x0000,0x00F0,0x0000,0x0C00,
  162.     0x0000,0x0108,0x0000,0x0C00,
  163.     0x0000,0x0108,0x0000,0x0C00,
  164.     0x4000,0x00F0,0x0000,0x0C00,
  165.     0x1000,0x0000,0x0000,0x0C00,
  166.     0x0400,0x0000,0x0000,0x0C00,
  167.     0x01FF,0xFFFF,0xFFFF,0xFC00,
  168.     0x0000,0x0000,0x0000,0x0000,
  169.     
  170.     0xFFFF,0xFFFF,0xFFF8,0x0000,
  171.     0xD555,0x5555,0x5556,0x0000,
  172.     0xD555,0x5000,0x5555,0x8000,
  173.     0xD555,0x47FF,0x9555,0x6000,
  174.     0xD555,0x5F03,0xE555,0x5000,
  175.     0xD555,0x3E55,0xF555,0x5000,
  176.     0xD555,0x3F55,0xF555,0x5000,
  177.     0xD555,0x3F55,0xF555,0x5000,
  178.     0xD555,0x5E53,0xF555,0x5000,
  179.     0xD555,0x4147,0xE555,0x5000,
  180.     0xD555,0x551F,0xD555,0x5000,
  181.     0xD555,0x557F,0x1555,0x5000,
  182.     0xD555,0x54FC,0x5555,0x5000,
  183.     0xD555,0x55E1,0x5555,0x5000,
  184.     0xD555,0x54F5,0x5555,0x5000,
  185.     0xD555,0x5505,0x5555,0x5000,
  186.     0xD555,0x54F5,0x5555,0x5000,
  187.     0xD555,0x54F5,0x5555,0x5000,
  188.     0x3555,0x5505,0x5555,0x5000,
  189.     0x0D55,0x5555,0x5555,0x5000,
  190.     0x0355,0x5555,0x5555,0x5000,
  191.     0x0000,0x0000,0x0000,0x0000,
  192.     0x0000,0x0000,0x0000,0x0000,
  193. };
  194.  
  195. struct Image GadgetRender=
  196. {
  197.     0,0,            /* LeftEdge, TopEdge */
  198.     0x36, 0x17,        /* Width, Height */
  199.     2,                /* Depth */
  200.     ImageData        /* ImageData */
  201. };
  202.  
  203. UBYTE *ToolTypes[]={"ACTION=USE",NULL};
  204.  
  205. struct DiskObject DefaultIcon=
  206. {
  207.     WB_DISKMAGIC,                    /* do_Magic */
  208.     WB_DISKVERSION,                    /* do_Version */
  209.     NULL,                            /* do_Gadget.NextGadget */
  210.     0,0,                            /* do_Gadget.LeftEdge, TopEdge */
  211.     0x36, 0x17,                        /* do_Gadget.Width, Height */
  212.     GFLG_GADGIMAGE|GFLG_GADGHBOX,    /* do_Gadget.Flags */
  213.     GACT_RELVERIFY|GACT_IMMEDIATE,    /* do_Gadget.Activation */
  214.     GTYP_BOOLGADGET,                /* do_Gadget.GadgetType */
  215.     &GadgetRender,                    /* do_Gadget.GadgetRender */
  216.     NULL,                            /* do_Gadget.SelectRender */
  217.     NULL,                            /* do_Gadget.GadgetText */
  218.     0,                                /* do_Gadget.MutualExclude */
  219.     NULL,                            /* do_Gadget.SpecialInfo */
  220.     0,                                /* do_Gadget.GadgetID */
  221.     (APTR)WB_DISKREVISION,            /* do_Gadget.UserData */
  222.     WBPROJECT,                        /* do_Type */
  223.     (UBYTE*)"14Bit_Calibration",    /* do_DefaultTool */
  224.     ToolTypes,                        /* do_ToolTypes */
  225.     NO_ICON_POSITION,                /* do_CurrentX */
  226.     NO_ICON_POSITION,                /* do_CurrentY */
  227.     NULL,                            /* do_DrawerData */
  228.     NULL,                            /* do_ToolWindow */
  229.     4096                            /* do_StackSize */
  230. };
  231.  
  232.  
  233. UBYTE *Template="FROM,EDIT/S,USE/S,SAVE/S,PUBSCREEN/K";
  234.  
  235. struct ArgArray
  236. {
  237.     UBYTE *aa_From;
  238.     ULONG aa_Edit;
  239.     ULONG aa_Use;
  240.     ULONG aa_Save;
  241.     UBYTE *aa_PubScreen;
  242. };
  243.  
  244. UBYTE PubScreenName[60]="Workbench";
  245.  
  246. struct ArgArray AA = 
  247. {
  248.     NULL,
  249.     FALSE,
  250.     FALSE,
  251.     FALSE,
  252.     NULL,
  253. };
  254.  
  255.  
  256. long __saveds main(void)
  257. {
  258.     BOOL Audio;
  259.     struct IntuiMessage *MyMsg,MsgCopy;
  260.     
  261.     MyProc=(struct Process*)FindTask(NULL);
  262.     if (MyProc->pr_CLI==NULL)
  263.     {
  264.         WaitPort(&MyProc->pr_MsgPort);
  265.         WBenchMsg=(struct WBStartup*)GetMsg(&MyProc->pr_MsgPort);
  266.     }
  267.     
  268.     if (OpenLibs())
  269.     {
  270.         if (!WBenchMsg)
  271.         {
  272.             struct RDArgs *RDArgs;
  273.             
  274.             if (!(RDArgs=ReadArgs(Template, (LONG*)&AA, NULL)))
  275.             {
  276.                 PrintFault(IoErr(),NULL);
  277.                 Active=FALSE;
  278.             }
  279.             else
  280.             {
  281.                 if (AA.aa_From)
  282.                 {
  283.                     strcpy(File,FilePart(AA.aa_From));
  284.                     strncpy(Drawer,AA.aa_From,(ULONG)FilePart(AA.aa_From)-(ULONG)AA.aa_From);
  285.                 }
  286.                 if (AA.aa_PubScreen)
  287.                 {
  288.                     strcpy(PubScreenName,AA.aa_PubScreen);
  289.                 }
  290.                 FreeArgs(RDArgs);
  291.             }
  292.         }
  293.         else
  294.         {
  295.             UWORD Arg=0;
  296.             struct DiskObject *DiskObject;
  297.             BPTR OldLock;
  298.             UBYTE *String;
  299.             
  300.             if (WBenchMsg->sm_NumArgs>1)
  301.             {
  302.                 Arg=1;
  303.                 NameFromLock(WBenchMsg->sm_ArgList[Arg].wa_Lock,Drawer,sizeof(Drawer));
  304.                 strcpy(File,WBenchMsg->sm_ArgList[Arg].wa_Name);
  305.             }
  306.             
  307.             OldLock=CurrentDir(WBenchMsg->sm_ArgList[Arg].wa_Lock);
  308.             if (DiskObject=GetDiskObject(WBenchMsg->sm_ArgList[Arg].wa_Name))
  309.             {
  310.                 if (String=FindToolType(DiskObject->do_ToolTypes,"ACTION"))
  311.                 {
  312.                     if (MatchToolValue(String,"EDIT"))
  313.                     {
  314.                         AA.aa_Edit=TRUE;
  315.                     }
  316.                     if (MatchToolValue(String,"USE"))
  317.                     {
  318.                         AA.aa_Use=TRUE;
  319.                     }
  320.                     if (MatchToolValue(String,"SAVE"))
  321.                     {
  322.                         AA.aa_Save=TRUE;
  323.                     }
  324.                 }
  325.                 if (String=FindToolType(DiskObject->do_ToolTypes,"CREATEICONS"))
  326.                 {
  327.                     if (MatchToolValue(String,"YES"))
  328.                     {
  329.                         CalibNewMenu[NEWICONINDEX].nm_Flags |= CHECKED;
  330.                         CreateIcons=TRUE;
  331.                     }
  332.                     if (MatchToolValue(String,"NO"))
  333.                     {
  334.                         CalibNewMenu[NEWICONINDEX].nm_Flags &= ~(CHECKED);
  335.                         CreateIcons=FALSE;
  336.                     }
  337.                 }
  338.                 if (String=FindToolType(DiskObject->do_ToolTypes,"PUBSCREEN"))
  339.                 {
  340.                     strcpy(PubScreenName,String);
  341.                 }
  342.                 FreeDiskObject(DiskObject);
  343.             }
  344.             CurrentDir(OldLock);
  345.         }
  346.         
  347.         if (Active)
  348.         {
  349.             BOOL Success=FALSE;
  350.             
  351.             if (strlen(File))
  352.             {
  353.                 if (!(Success=LoadData(Drawer,File)))
  354.                 {
  355.                     Message("Error loading calibration from file '%s'\nin directory '%s'","Okay",File,Drawer);
  356.                 }
  357.             }
  358.             
  359.             if (!Success)
  360.             {
  361.                 Success=LoadData("ENV:CyberSound/SoundDrivers","14Bit_Calibration");
  362.             }
  363.             
  364.             if (!Success)
  365.             {
  366.                 DefaultData();
  367.             }
  368.             
  369.             if (!(AA.aa_Use || AA.aa_Save)) AA.aa_Edit=TRUE;
  370.             
  371.             if (AA.aa_Edit)
  372.             {
  373.                 while (!(Audio=AllocAudio()))
  374.                 {
  375.                     if (!Message("ERROR: Cannot allocate the audio channels!\n"
  376.                             "Quit all programs using the audio hardware and try again.","Retry|Cancel"))
  377.                     {
  378.                         break;
  379.                     }
  380.                 }
  381.                 
  382.                 if (Audio)
  383.                 {
  384.                     if (InitScreenWindow())
  385.                     {
  386.                         Resize();
  387.                         
  388.                         StartCalibSound(Freq);
  389.                         
  390.                         Message("CyberSound 14 bit driver calibration program\n"
  391.                                 "Copyright ©1995 by Christian Buchner\n"
  392.                                 "\n"
  393.                                 "Please note:\n"
  394.                                 "------------\n"
  395.                                 "I am NOT responsible for any damage that may happen to your\n"
  396.                                 "HIFI equipment. Use this software at your own risk!\n"
  397.                                 "\n"
  398.                                 "Follow these instructions and noting harmful will happen\n"
  399.                                 "to your HIFI set.\n"
  400.                                 "\n"
  401.                                 "The audio channels have been allocated at maximum priority.\n"
  402.                                 "No system compliant program can interfere.","I see");
  403.                         
  404.                         Message("Instructions for calibrating the 14 bit driver\n"
  405.                                 "----------------------------------------------\n"
  406.                                 "Turn up the volume of your amplifier until you can hear a beep.\n"
  407.                                 "Use the cursor left-right keys to move the cursor (white line) in\n"
  408.                                 "the spectrum. Use the cursor up-down keys for calibration.\n"
  409.                                 "\n"
  410.                                 "Your aim is to make the beep disappear as much as possible in all\n"
  411.                                 "positions of the spectrum.\n"
  412.                                 "\n"
  413.                                 "The + / - keys change the frequency of the calibration tone.\n"
  414.                                 "\n"
  415.                                 "Save your settings when finished.\n"
  416.                                 "\n"
  417.                                 "THEN TURN DOWN THE VOLUME OF YOUR AMPLIFIER!\n"
  418.                                 "You will be warned again before the program exits.","I see");
  419.                         
  420.                         while(Active)
  421.                         {
  422.                             HighSample=(((pos-128)&0xFF)<<8)+((pos-127)&0xFF);
  423.                             LowSample =((((UWORD)Array[pos])&0xFF)<<8);
  424.                             
  425.                             sprintf(buffer1,"%04lx",(ULONG)HighSample);
  426.                             GT_SetGadgetAttrs(HighGad, Window, NULL, GTTX_Text, buffer1, TAG_DONE);
  427.                             sprintf(buffer2,"%05lx",(ULONG)LowSample );
  428.                             GT_SetGadgetAttrs(LowGad,  Window, NULL, GTTX_Text, buffer2, TAG_DONE);
  429.                             
  430.                             while (!(MyMsg=GT_GetIMsg(Window->UserPort)))
  431.                             {
  432.                                 Wait(1L<<Window->UserPort->mp_SigBit);
  433.                             }
  434.                             CopyMem((APTR)MyMsg,(APTR)&MsgCopy,sizeof(struct IntuiMessage));
  435.                             GT_ReplyIMsg(MyMsg);
  436.                             
  437.                             if (MsgCopy.Class==IDCMP_RAWKEY)
  438.                             {
  439.                                 /* Cursor right */
  440.                                 if (MsgCopy.Code==0x4e)
  441.                                 {
  442.                                     if (pos<254)
  443.                                     {
  444.                                         DrawBar(Window,pos,Array[pos],Array[pos],3);
  445.                                         pos++;
  446.                                         DrawBar(Window,pos,Array[pos],Array[pos],2);
  447.                                     }
  448.                                 }
  449.                                 /* Cursor left */
  450.                                 if (MsgCopy.Code==0x4f)
  451.                                 {
  452.                                     if (pos>0)
  453.                                     {
  454.                                         DrawBar(Window,pos,Array[pos],Array[pos],3);
  455.                                         pos--;
  456.                                         DrawBar(Window,pos,Array[pos],Array[pos],2);
  457.                                     }
  458.                                 }
  459.                                 /* Cursor up */
  460.                                 if (MsgCopy.Code==0x4c)
  461.                                 {
  462.                                     if (Array[pos]<127)
  463.                                     {
  464.                                         DrawBar(Window,pos,Array[pos]+1,Array[pos],2);
  465.                                         Array[pos]++;
  466.                                     }
  467.                                 }
  468.                                 /* Cursor down */
  469.                                 if (MsgCopy.Code==0x4d)
  470.                                 {
  471.                                     if (Array[pos]>-128)
  472.                                     {
  473.                                         DrawBar(Window,pos,Array[pos]-1,Array[pos],2);
  474.                                         Array[pos]--;
  475.                                     }
  476.                                 }
  477.                             }
  478.                             if (MsgCopy.Class==IDCMP_VANILLAKEY)
  479.                             {
  480.                                 /* + : Increment frequency */
  481.                                 if (MsgCopy.Code=='+')
  482.                                 {
  483.                                     if (Freq<20000)
  484.                                     {
  485.                                         Freq+=100;
  486.                                         StartCalibSound(Freq);
  487.                                     }
  488.                                 }
  489.                                 /* - : Decrement frequency */
  490.                                 if (MsgCopy.Code=='-')
  491.                                 {
  492.                                     if (Freq>100)
  493.                                     {
  494.                                         Freq-=100;
  495.                                         StartCalibSound(Freq);
  496.                                     }
  497.                                 }
  498.                             }
  499.                             if (MsgCopy.Class==IDCMP_NEWSIZE)
  500.                             {
  501.                                 Resize();
  502.                             }
  503.                             if (MsgCopy.Class==IDCMP_GADGETUP)
  504.                             {
  505.                                 struct Gadget *IA=(struct Gadget*)MyMsg->IAddress;
  506.                                 if (IA->GadgetID==1)
  507.                                 {
  508.                                     AA.aa_Save=TRUE;
  509.                                     AA.aa_Use=FALSE;
  510.                                     Active=FALSE;
  511.                                 }
  512.                                 if (IA->GadgetID==2)
  513.                                 {
  514.                                     AA.aa_Save=FALSE;
  515.                                     AA.aa_Use=TRUE;
  516.                                     Active=FALSE;
  517.                                 }
  518.                                 if (IA->GadgetID==3)
  519.                                 {
  520.                                     AA.aa_Save=FALSE;
  521.                                     AA.aa_Use=FALSE;
  522.                                     Active=FALSE;
  523.                                 }
  524.                             }
  525.                             if (MsgCopy.Class==IDCMP_MENUPICK)
  526.                             {
  527.                                 struct MenuItem *n;
  528.                                 void (*func)(void);
  529.                                 
  530.                                 while( MsgCopy.Code != MENUNULL )
  531.                                 {
  532.                                     n = ItemAddress( Menu, MsgCopy.Code );
  533.                                     func = (void *)(GTMENUITEM_USERDATA( n ));
  534.                                     if (func) func();
  535.                                     MsgCopy.Code = n->NextSelect;
  536.                                 }
  537.                             }
  538.                         }
  539.                         
  540.                         Message("*** WARNING! ***\n"
  541.                                 "TURN DOWN THE VOLUME OF YOUR AMPLIFIER BACK TO NORMAL VOLUME!\n"
  542.                                 ">>> NOW! <<<\n"
  543.                                 "This will prevent serious damage of your HIFI equipment.",
  544.                                 "Yes, the volume is down");
  545.                         
  546.                         StopCalibSound();
  547.                         
  548.                         DeinitScreenWindow();
  549.                     }
  550.                     FreeAudio();
  551.                 }
  552.             }
  553.         }
  554.         
  555.         if (AA.aa_Use || AA.aa_Save)
  556.         {
  557.             SaveData("ENV:",NULL,"14Bit_Calibration",CreateIcons);
  558.             
  559.             if (AA.aa_Save)
  560.             {
  561.                 SaveData("ENVARC:",NULL,"14Bit_Calibration",CreateIcons);
  562.             }
  563.         }
  564.         
  565.         CloseLibs();
  566.     }
  567.     
  568.     if (WBenchMsg)
  569.     {
  570.         Forbid();
  571.         ReplyMsg(WBenchMsg);
  572.     }
  573. }
  574.  
  575.  
  576. void OpenConfig(void)
  577. {
  578.     APTR requester;
  579.     BOOL Success=FALSE;
  580.     
  581.     if (requester=AllocAslRequestTags(ASL_FileRequest, TAG_DONE))
  582.     {
  583.         if (AslRequestTags(requester,    ASLFR_Window, Window,
  584.                                         ASLFR_InitialDrawer, Drawer,
  585.                                         ASLFR_InitialFile, File,
  586.                                         ASLFR_TitleText, "Load calibration...",
  587.                                         TAG_DONE))
  588.         {
  589.             strcpy(Drawer,((struct FileRequester*)requester)->fr_Drawer);
  590.             strcpy(File,((struct FileRequester*)requester)->fr_File);
  591.             
  592.             if (!(Success=LoadData(Drawer,File)))
  593.             {
  594.                 Message("Error loading calibration from file '%s'\nin directory '%s'","Okay",File,Drawer);
  595.             }
  596.             
  597.             if (!Success)
  598.             {
  599.                 Success=LoadData("ENV:CyberSound/SoundDrivers","14Bit_Calibration");
  600.             }
  601.             
  602.             if (!Success)
  603.             {
  604.                 DefaultData();
  605.             }
  606.             
  607.             ShowData();
  608.             
  609.         }
  610.         FreeAslRequest(requester);
  611.     }
  612. }
  613.  
  614.  
  615. void SaveAs(void)
  616. {
  617.     APTR requester;
  618.     
  619.     if (requester=AllocAslRequestTags(ASL_FileRequest, TAG_DONE))
  620.     {
  621.         if (AslRequestTags(requester,    ASLFR_Window, Window,
  622.                                         ASLFR_InitialDrawer, Drawer,
  623.                                         ASLFR_InitialFile, File,
  624.                                         ASLFR_TitleText, "Save calibration as...",
  625.                                         ASLFR_DoSaveMode, TRUE,
  626.                                         TAG_DONE))
  627.         {
  628.             strcpy(Drawer,((struct FileRequester*)requester)->fr_Drawer);
  629.             strcpy(File,((struct FileRequester*)requester)->fr_File);
  630.             
  631.             if (!SaveData(NULL,Drawer,File,CreateIcons))
  632.             {
  633.                 Message("Error saving calibration to file '%s'\nin directory '%s'","Okay",File,Drawer);
  634.             }
  635.         }
  636.         FreeAslRequest(requester);
  637.     }
  638. }
  639.  
  640.  
  641. void Quit(void)
  642. {
  643.     Active=FALSE;
  644. }
  645.  
  646.  
  647. void ResetToDefaults(void)
  648. {
  649.     DefaultData();
  650.     ShowData();
  651. }
  652.  
  653.  
  654. void LastSaved(void)
  655. {
  656.     if (!LoadData("ENVARC:CyberSound/SoundDrivers","14Bit_Calibration"))
  657.     {
  658.         DisplayBeep(NULL);
  659.         DefaultData();
  660.     }
  661.     ShowData();
  662. }
  663.  
  664.  
  665. void Restore(void)
  666. {
  667.     if (!LoadData("ENV:CyberSound/SoundDrivers","14Bit_Calibration"))
  668.     {
  669.         DisplayBeep(NULL);
  670.         DefaultData();
  671.     }
  672.     ShowData();
  673. }
  674.  
  675.  
  676. void IconToggle(void)
  677. {
  678.     CreateIcons=ItemAddress(Menu,ICONITEM)->Flags&CHECKED ? TRUE : FALSE;
  679. }
  680.  
  681.  
  682. BOOL OpenLibs(void)
  683. {
  684.     BOOL Success=FALSE;
  685.     
  686.     if (DOSBase=(struct DosLibrary *)OpenLibrary("dos.library",37))
  687.     {
  688.         if (IntuitionBase=(struct IntuitionBase*)OpenLibrary("intuition.library",37))
  689.         {
  690.             if (GfxBase=(struct GfxBase*)OpenLibrary("graphics.library",37))
  691.             {
  692.                 if (GadToolsBase=(struct Library*)OpenLibrary("gadtools.library",37))
  693.                 {
  694.                     if (IconBase=OpenLibrary("icon.library",37))
  695.                     {
  696.                         if (!(AslBase=OpenLibrary("asl.library",37)))
  697.                         {
  698.                             Message("Cannot open asl library!","Okay");
  699.                         }
  700.                         else
  701.                         {
  702.                             Success=TRUE;
  703.                         }
  704.                     }
  705.                 }
  706.             }
  707.         }
  708.     }
  709.     if (!Success)
  710.     {
  711.         CloseLibs();
  712.     }
  713.     return(Success);
  714. }
  715.  
  716.  
  717. void CloseLibs(void)
  718. {
  719.     if (AslBase)
  720.     {
  721.         CloseLibrary(AslBase);
  722.         AslBase=NULL;
  723.     }
  724.     if (IconBase)
  725.     {
  726.         CloseLibrary(IconBase);
  727.         IconBase=NULL;
  728.     }
  729.     if (GadToolsBase)
  730.     {
  731.         CloseLibrary(GadToolsBase);
  732.         GadToolsBase=NULL;
  733.     }
  734.     if (GfxBase)
  735.     {
  736.         CloseLibrary((struct Library*)GfxBase);
  737.         GfxBase=NULL;
  738.     }
  739.     if (IntuitionBase)
  740.     {
  741.         CloseLibrary((struct Library*)IntuitionBase);
  742.         IntuitionBase=NULL;
  743.     }
  744.     if (DOSBase)
  745.     {
  746.         CloseLibrary((struct Library*)DOSBase);
  747.         DOSBase=NULL;
  748.     }
  749. }
  750.  
  751.  
  752. BOOL InitScreenWindow(void)
  753. {
  754.     BOOL Success=FALSE;
  755.     
  756.     if (!(Screen=LockPubScreen(PubScreenName)))
  757.     {
  758.         Message("Unable to lock public screen '%s'","Okay",PubScreenName);
  759.     }
  760.     else
  761.     {
  762.         ScreenToFront(Screen);
  763.         
  764.         if (VisualInfo=GetVisualInfo(Screen,TAG_DONE))
  765.         {
  766.             Forbid();
  767.             Font = &Attr;
  768.             Font->ta_Name = (STRPTR)GfxBase->DefaultFont->tf_Message.mn_Node.ln_Name;
  769.             Font->ta_YSize = FontY = GfxBase->DefaultFont->tf_YSize;
  770.             FontX = GfxBase->DefaultFont->tf_XSize;
  771.             Permit();
  772.             
  773.             GadHeight=FontY+7;
  774.             GadSpace=2*GadHeight+12;
  775.             
  776.             if (!(Window=OpenWindowTags(NULL,    WA_Title,"14 bit driver calibration",
  777.                                                 WA_PubScreen, Screen,
  778.                                                 WA_InnerWidth, 255+20,
  779.                                                 WA_InnerHeight,128+GadSpace+12,
  780.                                                 WA_IDCMP, IDCMP_VANILLAKEY|IDCMP_RAWKEY|IDCMP_NEWSIZE|IDCMP_MENUPICK|BUTTONIDCMP,
  781.                                                 WA_SizeGadget, TRUE,
  782.                                                 WA_DragBar,TRUE,
  783.                                                 WA_DepthGadget, TRUE,
  784.                                                 WA_Activate, TRUE,
  785.                                                 WA_MaxWidth,-1,
  786.                                                 WA_MaxHeight,-1,
  787.                                                 WA_NewLookMenus,TRUE,
  788.                                                 TAG_END)))
  789.             {
  790.                 Message("Unable to open window.","Okay");
  791.             }
  792.             else
  793.             {
  794.                 OldWindowPtr=MyProc->pr_WindowPtr;
  795.                 MyProc->pr_WindowPtr=Window;
  796.                 
  797.                 if (Menu=(struct Menu *)CreateMenus(CalibNewMenu, TAG_DONE))
  798.                 {
  799.                     LayoutMenus(Menu, VisualInfo, GTMN_NewLookMenus, TRUE, TAG_DONE);
  800.                     SetMenuStrip(Window, Menu);
  801.                     
  802.                     Success=TRUE;
  803.                 }
  804.             }
  805.         }
  806.     }
  807.     return(Success);
  808. }
  809.  
  810.  
  811. void DeinitScreenWindow(void)
  812. {
  813.     FreeButtons();
  814.     
  815.     if (Window)
  816.     {
  817.         MyProc->pr_WindowPtr=OldWindowPtr;
  818.         ClearMenuStrip(Window);
  819.         if (Menu)
  820.         {
  821.             FreeMenus(Menu);
  822.             Menu=NULL;
  823.         }
  824.         CloseWindow(Window);
  825.         Window=NULL;
  826.     }
  827.     if (VisualInfo)
  828.     {
  829.         FreeVisualInfo(VisualInfo);
  830.         VisualInfo=NULL;
  831.     }
  832.     if (Screen)
  833.     {
  834.         UnlockPubScreen(NULL,Screen);
  835.         Screen=NULL;
  836.     }
  837. }
  838.  
  839.  
  840. void Resize(void)
  841. {
  842.     struct NewGadget NG;
  843.     struct Gadget *g;
  844.     UWORD GadWidth;
  845.     
  846.     FreeButtons();
  847.     
  848.     EraseRect(Window->RPort,Window->BorderLeft,Window->BorderTop,Window->Width-Window->BorderRight-1,Window->Height-Window->BorderBottom-1);
  849.     RefreshWindowFrame(Window);
  850.     
  851.     AreaLeft=Window->BorderLeft+10;
  852.     AreaTop=Window->BorderTop+6;
  853.     AreaWidth=Window->Width-Window->BorderLeft-Window->BorderRight-20;
  854.     AreaHeight=Window->Height-Window->BorderTop-Window->BorderBottom-12-GadSpace;
  855.     
  856.     GadWidth=AreaWidth/4;
  857.     
  858.     DrawBevelBox(Window->RPort,AreaLeft-2,AreaTop-2,AreaWidth+4,AreaHeight+4,GTBB_Recessed,TRUE,GT_VisualInfo,VisualInfo,TAG_DONE);
  859.     
  860.     if (g=CreateContext(&Context))
  861.     {
  862.         NG.ng_LeftEdge=AreaLeft-2+4*FontX+10;
  863.         NG.ng_TopEdge=AreaTop+AreaHeight+7;
  864.         NG.ng_Width=GadWidth;
  865.         NG.ng_Height=GadHeight;
  866.         NG.ng_GadgetText="High";
  867.         NG.ng_TextAttr=Font;
  868.         NG.ng_GadgetID=0;
  869.         NG.ng_Flags=PLACETEXT_LEFT;
  870.         NG.ng_VisualInfo=VisualInfo;
  871.         NG.ng_UserData=NULL;
  872.         
  873.         if (g=HighGad=CreateGadget(TEXT_KIND, g, &NG, GTTX_Border, TRUE, GTTX_Justification, GTJ_CENTER, TAG_DONE))
  874.         {
  875.             NG.ng_LeftEdge=AreaLeft+AreaWidth-NG.ng_Width+2;
  876.             NG.ng_GadgetText="Low";
  877.             
  878.             if (g=LowGad=CreateGadget(TEXT_KIND, g, &NG, GTTX_Border, TRUE, GTTX_Justification, GTJ_CENTER, TAG_DONE))
  879.             {
  880.                 NG.ng_LeftEdge=AreaLeft-2;
  881.                 NG.ng_TopEdge=AreaTop+AreaHeight+(GadSpace-GadHeight)+1;
  882.                 NG.ng_Width=GadWidth;
  883.                 NG.ng_Height=GadHeight;
  884.                 NG.ng_GadgetText="Save";
  885.                 NG.ng_GadgetID=1;
  886.                 NG.ng_Flags=PLACETEXT_IN;
  887.                 
  888.                 if (g=CreateGadget(BUTTON_KIND, g, &NG, TAG_DONE))
  889.                 {
  890.                     NG.ng_LeftEdge=AreaLeft+AreaWidth/2-GadWidth/2;
  891.                     NG.ng_GadgetText="Use";
  892.                     NG.ng_GadgetID=2;
  893.                     
  894.                     if (g=CreateGadget(BUTTON_KIND, g, &NG, TAG_DONE))
  895.                     {
  896.                         NG.ng_LeftEdge=AreaLeft+AreaWidth-GadWidth+2;
  897.                         NG.ng_GadgetText="Cancel";
  898.                         NG.ng_GadgetID=3;
  899.                         
  900.                         if (g=CreateGadget(BUTTON_KIND, g, &NG, TAG_DONE))
  901.                         {
  902.                         }
  903.                     }
  904.                 }
  905.             }
  906.         }
  907.         
  908.         AddGList(Window, Context, -1, -1, NULL);
  909.         RefreshGList(Context, Window, NULL, -1);
  910.         GT_RefreshWindow(Window, NULL);
  911.     }
  912.     ShowData();
  913. }
  914.  
  915.  
  916. void FreeButtons(void)
  917. {
  918.     if (Context)
  919.     {
  920.         RemoveGList(Window,Context,-1);
  921.         FreeGadgets(Context);
  922.         Context=NULL;
  923.     }
  924. }
  925.  
  926.  
  927. void DrawBar(struct Window *Window, UBYTE bar, BYTE value, BYTE oldvalue, UWORD pen)
  928. {
  929.     UWORD temp, ymin, ymax;
  930.     
  931.     UWORD Mid=AreaTop+(AreaHeight-1)/2;
  932.     UWORD Left=AreaLeft+(ULONG)bar*AreaWidth/255;
  933.     UWORD Right=AreaLeft+(ULONG)(bar+1)*AreaWidth/255-1;
  934.     
  935.     SetAPen(Window->RPort,pen);
  936.     
  937.     if (abs(value)<abs(oldvalue))
  938.     {
  939.         ymin=Mid-(LONG)oldvalue*AreaHeight/256;
  940.         ymax=Mid-(LONG)value*AreaHeight/256;
  941.         
  942.         if (ymin>ymax) {temp=ymax;ymax=ymin;ymin=temp;}
  943.         EraseRect(Window->RPort,Left,ymin,Right,ymax);
  944.     }
  945.     
  946.     ymin=Mid;
  947.     ymax=Mid-(LONG)value*AreaHeight/256;
  948.     
  949.     if (ymin>ymax) {temp=ymax;ymax=ymin;ymin=temp;}
  950.     RectFill(Window->RPort,Left,ymin,Right,ymax);
  951. }
  952.  
  953.  
  954. BOOL AllocAudio(void)
  955. {
  956.     BOOL Success=FALSE;
  957.     
  958.     UBYTE ChannelMap[]={0xf};
  959.     
  960.     if (AudioReply=CreateMsgPort())
  961.     {
  962.         if (AudioRequest=CreateIORequest(AudioReply,sizeof(struct IOAudio)))
  963.         {
  964.             AudioRequest->ioa_Request.io_Message.mn_Node.ln_Pri=127;
  965.             AudioRequest->ioa_Data=ChannelMap;
  966.             AudioRequest->ioa_Length=sizeof(ChannelMap);
  967.             AudioRequest->ioa_Request.io_Flags=ADIOF_NOWAIT;
  968.             
  969.             if (!OpenDevice("audio.device",0,(struct IORequest*)AudioRequest,0))
  970.             {
  971.                 Success=TRUE;
  972.             }
  973.         }
  974.     }
  975.     if (!Success) FreeAudio();
  976.     return(Success);
  977. }
  978.  
  979.  
  980. void FreeAudio(void)
  981. {
  982.     if (AudioRequest)
  983.     {
  984.         if (AudioRequest->ioa_Request.io_Device)
  985.         {
  986.             CloseDevice((struct IORequest*)AudioRequest);
  987.             AudioRequest->ioa_Request.io_Device=NULL;
  988.         }
  989.         DeleteIORequest(AudioRequest);
  990.         AudioRequest=NULL;
  991.     }
  992.     if (AudioReply)
  993.     {
  994.         DeleteMsgPort(AudioReply);
  995.         AudioReply=NULL;
  996.     }
  997. }
  998.  
  999.  
  1000. void StartCalibSound(ULONG Frequency)
  1001. {
  1002.     struct Custom *custom = (struct Custom*)0xdff000;
  1003.     struct CIA *ciaa=(struct CIA*)0xbfe001;
  1004.     
  1005.     ciaa->ciapra |= CIAF_LED;
  1006.     custom->dmacon=DMAF_AUDIO;
  1007.     custom->adkcon=    ADKF_USE3PN|ADKF_USE2P3|
  1008.                     ADKF_USE1P2|ADKF_USE0P1|
  1009.                     ADKF_USE3VN|ADKF_USE2V3|
  1010.                     ADKF_USE1V2|ADKF_USE0V1;
  1011.     custom->intena=INTF_AUD0|INTF_AUD1|INTF_AUD2|INTF_AUD3;
  1012.     
  1013.     Delay(1);
  1014.     
  1015.     custom->aud[0].ac_ptr=&HighSample;
  1016.     custom->aud[1].ac_ptr=&HighSample;
  1017.     custom->aud[2].ac_ptr=&LowSample;
  1018.     custom->aud[3].ac_ptr=&LowSample;
  1019.     
  1020.     custom->aud[0].ac_vol=
  1021.     custom->aud[1].ac_vol=64;
  1022.     custom->aud[2].ac_vol=
  1023.     custom->aud[3].ac_vol=1;
  1024.     
  1025.     custom->aud[0].ac_len=
  1026.     custom->aud[1].ac_len=
  1027.     custom->aud[2].ac_len=
  1028.     custom->aud[3].ac_len=1;
  1029.     
  1030.     custom->aud[0].ac_per=
  1031.     custom->aud[1].ac_per=
  1032.     custom->aud[2].ac_per=
  1033.     custom->aud[3].ac_per=(*(struct ExecBase**)(4))->ex_EClockFrequency*5/Frequency;;
  1034.     
  1035.     custom->dmacon=DMAF_SETCLR|DMAF_AUDIO;
  1036. }
  1037.  
  1038.  
  1039. void StopCalibSound(void)
  1040. {
  1041.     struct Custom *custom = (struct Custom*)0xdff000;
  1042.     
  1043.     custom->dmacon=DMAF_SETCLR|DMAF_AUDIO;
  1044.     custom->aud[0].ac_vol=
  1045.     custom->aud[1].ac_vol=
  1046.     custom->aud[2].ac_vol=
  1047.     custom->aud[3].ac_vol=0;
  1048. }
  1049.  
  1050.  
  1051. BOOL LoadData(UBYTE *Dirname, UBYTE *Filename)
  1052. {
  1053.     BOOL Success=FALSE;
  1054.     BPTR FH;
  1055.     BPTR DirLock,OldDir;
  1056.     
  1057.     if (DirLock=Lock(Dirname,ACCESS_READ))
  1058.     {
  1059.         OldDir=CurrentDir(DirLock);
  1060.         if (FH=Open(Filename,MODE_OLDFILE))
  1061.         {
  1062.             if (Read(FH,AdditiveArray,sizeof(AdditiveArray))==sizeof(AdditiveArray))
  1063.             {
  1064.                 Success=TRUE;
  1065.             }
  1066.             Close(FH);
  1067.         }
  1068.         CurrentDir(OldDir);
  1069.         UnLock(DirLock);
  1070.     }
  1071.     
  1072.     return(Success);
  1073. }
  1074.  
  1075.  
  1076. BOOL SaveData(UBYTE *EnvDir, UBYTE *Dirname, UBYTE *Filename, BOOL CreateIcon)
  1077. {
  1078.     BOOL Success=FALSE;
  1079.     BPTR OldDir=NULL;
  1080.     BPTR DirLock=NULL;
  1081.     BPTR EnvLock=NULL,CybLock=NULL,SndLock=NULL;
  1082.     BPTR FH;
  1083.     
  1084.     if (EnvDir)
  1085.     {
  1086.         if (EnvLock=Lock(EnvDir,ACCESS_READ))
  1087.         {
  1088.             OldDir=CurrentDir(EnvLock);
  1089.             
  1090.             if (!(CybLock=Lock("CyberSound",ACCESS_READ)))
  1091.             {
  1092.                 CybLock=CreateDir("CyberSound");
  1093.             }
  1094.             if (CybLock)
  1095.             {
  1096.                 CurrentDir(CybLock);
  1097.                 
  1098.                 if (!(SndLock=Lock("SoundDrivers",ACCESS_READ)))
  1099.                 {
  1100.                     SndLock=CreateDir("SoundDrivers");
  1101.                 }
  1102.                 if (SndLock)
  1103.                 {
  1104.                     CurrentDir(SndLock);
  1105.                     Success=TRUE;
  1106.                 }
  1107.             }
  1108.         }
  1109.     }
  1110.     else
  1111.     {
  1112.         if (DirLock=Lock(Dirname,ACCESS_READ))
  1113.         {
  1114.             OldDir=CurrentDir(DirLock);
  1115.             Success=TRUE;
  1116.         }
  1117.     }
  1118.     
  1119.     if (Success)
  1120.     {
  1121.         Success=FALSE;
  1122.         
  1123.         if (FH=Open(Filename,MODE_NEWFILE))
  1124.         {
  1125.             if (Write(FH,AdditiveArray,sizeof(AdditiveArray))==sizeof(AdditiveArray))
  1126.             {
  1127.                 if (CreateIcon)
  1128.                 {
  1129.                     PutDiskObject(Filename,&DefaultIcon);
  1130.                 }
  1131.                 Success=TRUE;
  1132.             }
  1133.             Close(FH);
  1134.         }
  1135.     }
  1136.     
  1137.     if (OldDir)  CurrentDir(OldDir);
  1138.     if (SndLock) UnLock(SndLock);
  1139.     if (CybLock) UnLock(CybLock);
  1140.     if (EnvLock) UnLock(EnvLock);
  1141.     if (DirLock) UnLock(DirLock);
  1142.     
  1143.     return(Success);
  1144. }
  1145.  
  1146.  
  1147. void DefaultData(void)
  1148. {
  1149.     UWORD i;
  1150.     for (i=0;i<255;i++) Array[i]=0x55;
  1151.     Array[255]=0x7f;
  1152. }
  1153.  
  1154.  
  1155. void ShowData(void)
  1156. {
  1157.     UWORD i;
  1158.     
  1159.     EraseRect(Window->RPort,AreaLeft,AreaTop,AreaLeft+AreaWidth-1,AreaTop+AreaHeight-1);
  1160.     
  1161.     for (i=0;i<255;i++)
  1162.     {
  1163.         if (i==pos)
  1164.             DrawBar(Window,i,Array[i],0,2);
  1165.         else
  1166.             DrawBar(Window,i,Array[i],0,3);
  1167.     }
  1168. }
  1169.  
  1170.  
  1171. /* Show a message to the user */
  1172.  
  1173. LONG __stdargs Message(UBYTE *Msg,UBYTE *Options,...)
  1174. {
  1175.     LONG retval;
  1176.     
  1177.     va_list Arg;
  1178.     struct EasyStruct Req={sizeof(struct EasyStruct),0,"14Bit Calibration",0, NULL};
  1179.     Req.es_TextFormat=Msg;
  1180.     Req.es_GadgetFormat=Options;
  1181.     va_start(Arg,Options);
  1182.     
  1183.     if (WBenchMsg || Window || strchr(Options,'|'))
  1184.     {
  1185.         retval=EasyRequestArgs(NULL,&Req,0,Arg);
  1186.     }
  1187.     else
  1188.     {
  1189.         VPrintf(Msg,Arg);
  1190.         Printf("\n");
  1191.         retval=0;
  1192.     }
  1193.     va_end(Arg);
  1194.     
  1195.     return(retval);
  1196. }
  1197.